package com.yidu.boss.sys.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yidu.boss.sys.domain.Custodian;
import com.yidu.boss.sys.domain.Trustee;
import com.yidu.boss.sys.domain.TrusteeExample;
import com.yidu.boss.sys.service.impl.TrusteeServiceImpl;
import com.yidu.boss.util.RandomString;
import org.apache.jasper.tagplugins.jstl.core.Out;
import org.apache.taglibs.standard.tag.common.sql.DataSourceUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther:si
 * @Date:2019/11/27
 * @Description:com.yidu.boss.sys.controller
 * @Version:1.0
 */
@Controller
@RequestMapping("/trustee/")
public class TrusteeController {
    //定义业务层
    @Autowired
    private TrusteeServiceImpl trusteeService;

    /**
     * 跳转托管人页面
     * @return 返回页面
     */
    @RequestMapping("trusteeJsp")
    public String toTrusteeJsp(){
        return "trustee";
    }

    /**
     * 查询所有给基金表使用
     * @return
     */
    @RequestMapping("selectAllTrustee")
    @ResponseBody
    public List<Trustee> selectAll(){
        List<Trustee> trusteeList =trusteeService.selectAll();
        return trusteeList;
    }



    /**
     * 带条件查询分页显示
     * @param page  页码
     * @param limit 页面大小
     * @return   返回分页数据
     */
    @RequestMapping("trusteelist")
    @ResponseBody
    public Object doSelectAllTrustee(Integer page,Integer limit){
        //得到页面大小和页码
        PageHelper.startPage(page,limit);
        //调用业务层方法(方法无参数用null)
        List<Trustee> trustees =trusteeService.selectByExample(null);
        //使用pageInfo包装查询后的结果，封装了详细的查询数据，其中参数是列表数据
        PageInfo<Trustee> trusteePageInfo =new PageInfo<>(trustees);
        //得到列表数据
        List<Trustee> trusteesData =trusteePageInfo.getList();
        //调用工具类的分页计数数据格式：map集合
        Map<String,Object> map = RandomString.pagingALG(trusteePageInfo,trusteePageInfo.getTotal());
        return map;
    }

    /**
     * 模糊查询所有托管人
     * @param condition
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("selectTrusteeLike")
    @ResponseBody
    public Object doSelectLikeAll(String condition,Integer page,Integer limit){
        //判断条件是否为空
        if(condition.equals("")){
            //条件为空，显示列表
            Object o =doSelectAllTrustee(page,limit);
            return o;
        }
        //开始页
        PageHelper.startPage(page,limit);
        //调用方法
        List<Trustee> trusteeList =trusteeService.selectSomeByMore(condition);
        //
        PageInfo<Trustee> trusteePageInfo =new PageInfo<>(trusteeList);
        //调用工具类的分页计数数据格式：map集合
        Map<String,Object> map =RandomString.pagingALG(trusteePageInfo,trusteePageInfo.getTotal());
        return map;
    }




    /**
     * 添加
     * @param trustee
     * @return
     */
    @RequestMapping("insertTrustee")
    @ResponseBody
    public String doAddTrustee(Trustee trustee){
        int i =trusteeService.insertTrustee(trustee);
        if(i > 0){
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 修改
     * @param trustee
     * @return
     */
    @RequestMapping("updateTrustee")
    @ResponseBody
    public String doUpdateTrustee(Trustee trustee){
        int i =trusteeService.updateTrustee(trustee);
        if( i>0){
            return "修改成功";
        }
        return "修改失败";
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @RequestMapping("deleteTrustee")
    @ResponseBody
    public String doDeleteTrustee(String[] ids){
        try{
            int i = 0;
            for(String id:ids){
                i += trusteeService.deleteTrustee(id);
            }
            if(i > 0){
                return "删除成功";
            }
        }catch (DataAccessException e){
            System.err.println("此主键有外键约束不能删除！！！");
        }
        return "删除失败";
    }

}
